import AppConstants from "../../framework/scripts/AppConstants"
import AppUtil from "../../framework/scripts/AppUtil"
import ResMgr from "../../framework/scripts/manager/ResMgr"
import LayerMgr from "../../framework/scripts/view/LayerMgr"


export default class Tip {
    private static _prefab: cc.Prefab
    private static _view: cc.Node


    public static async init() {
        if (this._prefab) return true

        const [prefab, err] = await AppUtil.asyncWrap<cc.Prefab>(ResMgr.loadRes('core/Tip'))
        if (err) {
            console.error(`Tip.init 找不到资源 [core/Tip] error:${err}`)
            return false
        }

        this._prefab = prefab

        return true
    }

    public static show(content: string) {
        if (!this._view || !this._view.isValid) {
            this._view = cc.instantiate(this._prefab)
            this._view.opacity = 0

            LayerMgr.setLayer(this._view, AppConstants.viewLayer.Top)
        }

        this._view.getChildByName('lblTip').getComponent(cc.Label).string = content

        this._view.y = 150
        this._view.stopAllActions()

        cc.tween(this._view).
            show().
            to(0.2, { opacity: 255 }).
            delay(0.5).
            to(0.5, { opacity: 0, y: 250 }).
            hide().
            start()
    }
}
